<?php 
    namespace Admin\Controller;

    class TopicsController extends AdminController
    {
        public function index()
        {
            //接收pid
            $pid = empty($_GET['pid'])?0:$_GET['pid'];

            //查询语句 查询当前话题的的内容
            $list = M('topics')->field('id, name, pid, path')->where('pid='.$pid)->select();

            // 查询当前分类的父话题的pid,作为返回上级的连接
            $ppid = M('topics')->field('pid')->where('id='.$pid)->select()[0]['pid'];

            // var_dump($list);
            $this->assign('ppid', $ppid);
            $this->assign('list', $list);
            $this->display('Topics/index');
        }


        public function add()
        {
            //直接产生PID
            //为空时候默认为 0 不为空时候就是传值
            $pid = empty($_GET['pid'])?0:$_GET['pid'];
            //直接产生path
            //为空的时候 默认为 '0,' 不为空的时候就是传值 拼接pid    + ,
            $path = empty($_GET['path'])?'0,':$_GET['path'].$pid.',';
            //添加子话题
            $this->assign('pid', $pid);
            $this->assign('path', $path);
            $this->display('Topics/add');
        }

        public function insert()
        {
            // 获取数据
            // 获取表单信息
            foreach ($_POST as $val) {
                if ($val == '') {
                    $this->error('表单有空值,请完善...');
                    exit;
                }
            }
            // 话题图片
            if (!empty($_FILES['imgFile']['name'])) {
                $image = new ImageController();
                $imageName = $image->up();
                $imageName45 = substr($imageName, 0, 11).'45_'.substr($imageName, 11);
                $imageName200 = substr($imageName, 0, 11).'200_'.substr($imageName, 11);

            } else {
                $this->error('图片为空...');
                exit;
            }
            // V($imageName);
            // exit;

            $data = array();
            $data['name'] = I('post.name');
            $data['pid'] = I('post.pid');
            $data['path'] = I('post.path');
            $data['detail'] = I('post.detail');
            $data['image'] = $imageName45; //主图

            if (M('topics')->data($data)->add()) {
                // 删除 原图 与 200_ 缩放 
                unlink('./Public/imgs/'.$imageName);
                unlink('./Public/imgs/'.$imageName200);
                $this->success('话题添加成功...','index/pid/'.$data['pid']);
            } else {
                //删除所有
                unlink('./Public/imgs/'.$imageName);
                unlink('./Public/imgs/'.$imageName45);
                unlink('./Public/imgs/'.$imageName200);
                $this->error('添加失败...');
            }
        }


        public function edit()
        {
            // 获取id数据
            if (empty($_GET['id']) || intval($_GET['id']) < 0) {
                $this->error('非法的id');
                exit;
            }
            $id = intval($_GET['id']);

            $data = M('topics')->where('id='.$id)->select();

            $this->assign('data', $data);
            $this->display('Topics/edit');
        }


        public function save()
        {
            // 获取数据
            // 获取表单信息
            foreach ($_POST as $val) {
                if ($val == '') {
                    $this->error('表单有空值,请完善...');
                    exit;
                }
            }

             // 获取id数据
            if (empty($_POST['id']) || intval($_POST['id']) < 0) {
                $this->error('非法的id...');
                exit;
            }
            $id = intval($_POST['id']);

            // 话题图片
            if (!empty($_FILES['imgFile']['name'])) {
                $image = new ImageController();
                $imageName = $image->up();
                $imageName45 = substr($imageName, 0, 11).'45_'.substr($imageName, 11);
                $imageName200 = substr($imageName, 0, 11).'200_'.substr($imageName, 11);
            } else {
                $this->error('图片为空...');
                exit;
            }
            // V($imageName);
            // exit;
            // 获取原图片名称
            $old_image = M('topics')->field('image')->where('id='.$id)->select()[0]['image'];

            $data = array();
            $data['name'] = I('post.name');
            $data['pid'] = I('post.pid');
            $data['path'] = I('post.path');
            $data['detail'] = I('post.detail');
            $data['image'] = $imageName45; //主图 45_

            if (M('topics')->where('id='.$id)->save($data)) {
                $this->success('话题编辑成功...','index/pid/'.$data['pid']);
                if ($old_image != '' && $old_image != 'default_80.jpg') {
                    unlink('./Public/imgs/'.$old_image);
                }
                // 删除原图与200
                unlink('./Public/imgs/'.$imageName);
                unlink('./Public/imgs/'.$imageName200);
            } else {
                // 删除所有缩放
                unlink('./Public/imgs/'.$imageName45);
                unlink('./Public/imgs/'.$imageName200);
                unlink('./Public/imgs/'.$imageName);
                $this->error('编辑失败...');
            }
        }


        public function del()
        {
            // 获取表单信息
            foreach ($_GET as $val) {
                if ($val == '') {
                    $this->error('表单有空值,请完善...');
                    exit;
                }
            }

            $map['id'] = I('get.id');
            $map['pid'] = I('get.pid');
            $path['path'] = I('get.path');

            // 判定条件
            // 顶级话题不能删除
            if(M('topics')->where('id='.$map['id'])->getField('pid') == 0) {
                $this->error('顶级话题,不能删除...');
                exit;
            }
            // 有下级话题的不能删除
            if(M('topics')->where('pid='.$map['id'])->limit(2)->select() != null) {
                $this->error('该话题有下级话题,不能删除...');
                exit;
            }

            // 话题下有问题的不能删除
            if (M('question')->where('topic_id='.$map['id'])->limit(2)->select()) {
                $this->error('该话题下有提问,不能删除...');
                exit;
            }

            //查询图片存在否
            $image = M('topics')->field('image')->where($map)->select();

            if (M('topics')->where($map)->delete()) {
                //图片存在则删除图片
                if (!empty($image[0]['image']) && $image[0]['image'] != 'default_80.jpg') {
                    unlink('./Public/imgs/'.$image[0]['image']);
                }
                $this->success('删除成功...', 'index/pid/'.$map['pid']);
            } else {
                $this->error('删除失败...');
                exit;
            }


        }








    }


